<?php

/**
 * CornerStone is a custom Theme class for the mzingi theme heavily modified to sut my needs.
 *
 * @package Habari
 */

// We must tell Habari to use this theme class as the custom theme class:
define( 'THEME_CLASS', 'contraira' );

/**
 * A custom theme for airoa output
 */
class contraira extends Theme
{

	public function action_init_theme()
	{
		//print_r( get_class( $this ) );
		//var_dump(debug_backtrace());
	
		// Apply Format::autop() to post content...
		//Format::apply( 'autop', 'post_content_out' );
		// Apply Format::autop() to post content...
		Format::apply( 'autop', 'post_content_excerpt' );
		// Apply Format::autop() to post content...
		Format::apply( 'autop', 'post_content_excerptlong' );
		// Apply Format::autop() to comment content...
		Format::apply( 'autop', 'comment_content_out' );
		// Apply Format::tag_and_list() to post tags...
		// Truncate content excerpt at "more" or 300 characters or 3 paragraphs...
		Format::apply( 'format_date', 'post_pubdate_out','{Y}-{m}-{d}' );

		if ( ! Options::get('contraira__configured' ) ){
			$this->set_default_options();
		}
		
	}

	public function add_template_vars()
	{			
		if( !$this->template_engine->assigned( 'pages' ) ) {
			$this->assign('pages', Posts::get( array( 'content_type' => Post::type('page'), 'status' => Post::status( 'published' ), 'limit' => 10 ) ) );
		}
		// For Asides loop in sidebar.php
		$this->assign( 'asides', Posts::get( array( 'tag'=>'aside', 'limit'=>10 ) ) );
		
		// for recent comments loop in sidebar.php
		$this->assign( 'recent_comments', Comments::get( array( 'limit'=>5, 'status'=>Comment::STATUS_APPROVED, 'orderby'=>'date DESC' ) ) );

		// visiting page/2, /3 will offset to the next page of posts in the sidebar
		$page =Controller::get_var( 'page' );
		if ( $page == '' ) { $page = 1; }
		$this->assign( 'more_posts', Posts::get( array ( 'status' => Post::status( 'published' ), 'content_type' => 'entry', 'offset' => $page, 'limit' => 100000 ) ) );
		
		
		Format::apply_with_hook_params( 'more', 'post_content_excerptlong',
			array( 	"more_text" => "Read more",
					"max_words" => 100,
					"max_paragraphs" => 1,
					"title" => true, 
					"class" => "full-post-link"
					 )
		);

		Format::apply_with_hook_params( 'more', 'post_content_project', '', 50, 1);
		$this->assign( 'project_posts', Posts::get( array ( 'status' => Post::status( 'published' ), 'content_type' => 'entry', 'limit' => 100000, 'tag' => 'project' ) ) );
		
		
		Format::apply_with_hook_params( 'more', 'post_content_excerpt', 'Read More &raquo;', 50, 1);
		parent::add_template_vars();
	}

	public function action_template_header( $theme )
	{
		//Stack::add( 'template_header_javascript', Site::get_url( 'scripts' ) . '/jquery.js', 'jquery' );
		//echo $this->contraira_css();
	}	
	
	
	public function act_display_home( $user_filters = array() )
	{
		$user_filters['content_type'] = array(
			Post::type( 'entry' ),
		);
		$user_filters['not:tag'] = array( 'aside' );
		$user_filters['limit'] = 1;
		
		return parent::act_display_home($user_filters);
	}
	
	public function act_display_entries( $user_filters = array() )
	{
		$user_filters['content_type'] = array(
			Post::type( 'entry' ),
		);
		$user_filters['not:tag'] = array( 'aside', );
		$user_filters['limit'] = 10;
		
		return parent::act_display_home($user_filters);
	}
	
	
	public function theme_body( $theme )
	{
		$body_class = array();

		$action = "display_home";
		if( !isset( $this->matched_rule->name ) ) return false;
		
		$action = $this->matched_rule->name;
		
		$body_class = 'page-home';
		
		switch( $action ) {
			case 'display_home':
			case 'display_entries':
			case 'display_404':
			case 'display_search':
				$body_class = 'page-home';
				break;
			case 'display_archive':
				$body_class = 'page-archives';
				break;
			case 'display_page':
				$body_class = 'page-' . $theme->post->slug;
				break;
			case 'display_entry':
				if( isset( $theme->post ) && is_array( $theme->post->tags ) && in_array( 'flickr_photos', $theme->post->tags ) ) {
					$body_class = 'page-photo';
				} else {
					$body_class = 'display_entry';
				}
				break;
			default:
				$body_class = 'page-default';
		}
		echo $body_class;	
	}

/* Lets add support for user specified background colors! */

	public function save_config( $form ){
		Session::notice( 'Contraira Settings Updated' );
		$form->save();
		if ( Cache::has('contraira_options')){
			Cache::expire('contraira_options');
		}
		return false;
	}

	public function action_form_comment( $form ) {
			$form->class[] = 'box';
			$form->append( 'fieldset', 'aboutyou', _t( 'A Little About You' ) );
			//$form->aboutyou->id = 'about-you';
	
			$form->move_before( $form->aboutyou, $form->cf_commenter );
			$form->cf_commenter->caption = _t('Name') . '*';
			$form->cf_commenter->move_into( $form->aboutyou );

			$form->cf_email->caption = _t('Email') . '*';
			$form->cf_email->move_into( $form->aboutyou );

			$form->cf_url->move_into( $form->aboutyou );

			$form->append( 'static', 'required', '<strong>*' . _t('Required and while be displayed') . '.</strong>' );
			$form->required->move_into( $form->aboutyou );
			$form->cf_submit->move_into( $form->aboutyou );

			$form->append( 'fieldset', 'textarea', _t( 'Add to the Discussion' ) );
			$form->move_before( $form->textarea, $form->cf_content );
			//$form->content->id = 'commentContent';
			//$form->content->caption = '';
			$form->cf_content->move_into( $form->textarea );
	}
	
	public function tags_out( $tags )
	{
		echo "<ul class=\"content\">\n";
		foreach( $tags as $tag ) {
			echo "<li><a href='" . URL::get( "display_entries_by_tag", array( "tag" => $tag  ) ) . "'>{$tag}</a></li>\n";
		}
		echo "</ul>\n";
	}
	


} // End of class


?>